package com.sfac.javaSpringBoot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 资源配置文件
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ResourceProperties resourceProperties;

    // 将本地文件夹注册为系统的静态资源文件夹
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //判断用户所使用系统
        String systemName = System.getProperty("os.name");
        if (systemName.toLowerCase().startsWith("win")) {
            registry.addResourceHandler(resourceProperties.getPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX +
                            resourceProperties.getWindowLocation());
        }
        else {
            registry.addResourceHandler(resourceProperties.getPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX +
                            resourceProperties.getLinuxLocation());
        }
    }
}
